package com.engineering.elink.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.engineering.elink.common.Constant;
import com.engineering.elink.common.ResultType;
import com.engineering.elink.domain.Certification;
import com.engineering.elink.domain.Feedback;
import com.engineering.elink.service.CookieService;
import com.engineering.elink.service.UserService;
import com.engineering.elink.util.MapConvert;
import com.engineering.elink.util.token.TokenGenerator;
import com.engineering.elink.util.verify.CodeImageGenerator;
import com.engineering.elink.util.verify.VerifyCodeUtils;

@RestController
@RequestMapping("/api/user")
@Api(value = "用户接口")
public class UserController {
	@Autowired
	private UserService userService;
	@Autowired
	private CookieService cookieService;

	/**
	 * 检查用户名是否存在 GET
	 * 
	 * @Param username 用户名
	 * @return result 1 用户名可用 0 用户名不可用
	 */
	@RequestMapping(value = "/checkEmail", method = RequestMethod.GET)
	@ApiOperation(value = "检验邮箱是否可用")
	public Map<String, Object> checkEmail(@ApiParam(value = "邮箱", required = true) @RequestParam("email") String email) {
		if (userService.checkEmail(email)) {
			return MapConvert.getMap(ResultType.SUCCESS);
		} else {
			return MapConvert.getMap(ResultType.EMAIL_EXIST);
		}
	}

	/**
	 * 验证手机号是否存在 GET
	 * 
	 * @Param phone 手机号
	 * @return result 1 手机号不可用 0
	 */
	@RequestMapping(value = "/checkPhone", method = RequestMethod.GET)
	@ApiOperation(value = "检验手机号是否可用")
	public Map<String, Object> checkPhone(@ApiParam(value = "手机号", required = true) @RequestParam("phone") String phone) {
		if (userService.checkPhone(phone)) {
			return MapConvert.getMap(ResultType.SUCCESS);
		} else {
			return MapConvert.getMap(ResultType.PHONE_EXIST);
		}
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/modifyPasswd", method = RequestMethod.POST)
	@ApiOperation(value = "修改密码")
	public Map<String, Object> modifyPasswd(@ApiParam(value = "原密码", required = true) @RequestParam(value = "passwd", required = true) String passwd,
			@ApiParam(value = "新密码", required = true) @RequestParam(value = "newPasswd", required = true) String newPasswd, HttpServletRequest request) {
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId")) {
			String userId = (String) userInfo.get("userId");
			userService.modifyPasswd(userId, passwd, newPasswd);
			return MapConvert.getMap(ResultType.SUCCESS);
		}
		return MapConvert.getMap(ResultType.REQUEST_PARAMS_ERROR);
	}

	@RequestMapping(value = "/modifyPhoneWithVerifyCode", method = RequestMethod.GET)
	@ApiOperation(value = "修改手机号流程一：获取验证码")
	public Map<String, Object> modifyPhoneWithVerifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String verifyToken = TokenGenerator.getToken();
		String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
		userService.checkAuthWithVerifyCode(Constant.checkMethodByPhone, verifyToken, verifyCode);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("img", CodeImageGenerator.generateBase64Image(verifyCode));
		map.put("verifyToken", verifyToken + "," + Constant.verifyCodeForTempValidTime);
		return MapConvert.getMap(ResultType.SUCCESS, map);
	}

	@RequestMapping(value = "/modifyPhoneWithSendPhoneCode", method = RequestMethod.POST)
	@ApiOperation(value = "修改手机号流程二：发送短信验证码")
	public Map<String, Object> modifyPhoneWithSendPhoneCode(HttpServletRequest request, HttpServletResponse response,
			@ApiParam(value = "手机号", required = true) @RequestParam(value = "phone", required = true) String phone,
			@ApiParam(value = "验证码", required = true) @RequestParam(value = "verifyCode", required = true) String verifyCode,
			@ApiParam(value = "令牌", required = true) @RequestParam(value = "verifyToken", required = true) String verifyToken) throws IOException {
		if (userService.checkAuthWithSendCode(phone, verifyToken, verifyCode,request)) {
			return MapConvert.getMap(ResultType.SUCCESS);
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);
	}

	@RequestMapping(value = "/modifyPhoneWithCheckPhoneCode", method = RequestMethod.POST)
	@ApiOperation(value = "修改手机号流程三：校验短信验证码")
	public Map<String, Object> modifyPhoneWithCheckPhoneCode(HttpServletRequest request, HttpServletResponse response,
			@ApiParam(value = "手机号", required = true) @RequestParam(value = "phone", required = true) String phone,
			@ApiParam(value = "短信验证码", required = true) @RequestParam(value = "verifyCode", required = true) String verifyCode,
			@ApiParam(value = "令牌", required = true) @RequestParam(value = "verifyToken", required = true) String verifyToken) throws IOException {
		if (userService.checkAuthWithCheckCode(phone, verifyToken, verifyCode)) {
			return MapConvert.getMap(ResultType.SUCCESS);
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);
	}

	@RequestMapping(value = "/modifyPhoneWithNewVerifyCode", method = RequestMethod.GET)
	@ApiOperation(value = "修改手机号流程四：获取新短信验证码")
	public Map<String, Object> modifyPhoneWithNewVerifyCode(
			@ApiParam(value = "令牌", required = true) @RequestParam(value = "verifyToken", required = true) String verifyToken,
			 HttpServletResponse response) throws IOException {
		String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
//		String verifyToken = cookieService.getCookie(request, "verifyToken");
		
		userService.modifyEmailOrPhoneWithVerifyCode(Constant.checkMethodByPhone, verifyToken, verifyCode);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("img", CodeImageGenerator.generateBase64Image(verifyCode));
		map.put("verifyToken", verifyToken + "," + Constant.verifyCodeForTempValidTime);
		return MapConvert.getMap(ResultType.SUCCESS, map);
	}

	@RequestMapping(value = "/modifyPhoneWithSendNewPhoneCode", method = RequestMethod.POST)
	@ApiOperation(value = "修改手机号流程五：发送新短信验证码")
	public Map<String, Object> modifyPhoneWithSendNewPhoneCode(HttpServletRequest request, HttpServletResponse response,
			@ApiParam(value = "新手机号", required = true) @RequestParam(value = "newPhone", required = true) String newPhone,
			@ApiParam(value = "验证码", required = true) @RequestParam(value = "verifyCode", required = true) String verifyCode,
			@ApiParam(value = "令牌", required = true) @RequestParam(value = "verifyToken", required = true) String verifyToken) throws IOException {
		if (userService.modifyEmailOrPhoneWithSendCode(newPhone, verifyToken, verifyCode,request)) {
			return MapConvert.getMap(ResultType.SUCCESS);
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);
	}

	@RequestMapping(value = "/modifyPhoneWithCheckNewPhoneCode", method = RequestMethod.POST)
	@ApiOperation(value = "修改手机号流程六：校验新短信验证码")
	public Map<String, Object> modifyPhoneWithCheckNewPhoneCode(HttpServletRequest request, HttpServletResponse response,
			@ApiParam(value = "新手机号", required = true) @RequestParam(value = "newPhone", required = true) String newPhone,
			@ApiParam(value = "短信验证码", required = true) @RequestParam(value = "verifyCode", required = true) String verifyCode,
			@ApiParam(value = "令牌", required = true) @RequestParam(value = "verifyToken", required = true) String verifyToken) throws IOException {
		if (userService.modifyEmailOrPhoneWithCheckCode(newPhone, verifyToken, verifyCode)) {
			return MapConvert.getMap(ResultType.SUCCESS);
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);
	}

	@RequestMapping(value = "/modifyEmailWithVerifyCode", method = RequestMethod.GET)
	@ApiOperation(value = "修改邮箱流程一：获取验证码")
	public Map<String, Object> modifyEmailWithVerifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String verifyToken = TokenGenerator.getToken();
		String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
		userService.checkAuthWithVerifyCode(Constant.checkMethodByPhone, verifyToken, verifyCode);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("img", CodeImageGenerator.generateBase64Image(verifyCode));
		map.put("verifyToken", verifyToken + "," + Constant.verifyCodeForTempValidTime);
		return MapConvert.getMap(ResultType.SUCCESS, map);
	}

	@RequestMapping(value = "/modifyEmailWithSendPhoneCode", method = RequestMethod.POST)
	@ApiOperation(value = "修改邮箱流程二：发送手机验证码")
	public Map<String, Object> modifyEmailWithSendPhoneCode(HttpServletRequest request, HttpServletResponse response,
			@ApiParam(value = "手机", required = true) @RequestParam(value = "phone", required = true) String phone,
			@ApiParam(value = "验证码", required = true) @RequestParam(value = "verifyCode", required = true) String verifyCode,
			@ApiParam(value = "令牌", required = true) @RequestParam(value = "verifyToken", required = true) String verifyToken) throws IOException {
		if (userService.checkAuthWithSendCode(phone, verifyToken, verifyCode,request)) {
			return MapConvert.getMap(ResultType.SUCCESS);
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);
	}

	@RequestMapping(value = "/modifyEmailWithCheckPhoneCode", method = RequestMethod.POST)
	@ApiOperation(value = "修改邮箱流程三：校验手机验证码")
	public Map<String, Object> modifyEmailWithCheckPhoneCode(HttpServletRequest request, HttpServletResponse response,
			@ApiParam(value = "手机", required = true) @RequestParam(value = "phone", required = true) String phone,
			@ApiParam(value = "手机验证码", required = true) @RequestParam(value = "verifyCode", required = true) String verifyCode,
			@ApiParam(value = "令牌", required = true) @RequestParam(value = "verifyToken", required = true) String verifyToken) throws IOException {
		if (userService.checkAuthWithCheckCode(phone, verifyToken, verifyCode)) {
			return MapConvert.getMap(ResultType.SUCCESS);
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);
	}

	@RequestMapping(value = "/modifyEmailWithNewVerifyCode", method = RequestMethod.GET)
	@ApiOperation(value = "修改邮箱流程四：获取新邮验证码")
	public Map<String, Object> modifyEmailWithNewVerifyCode(
			@ApiParam(value = "令牌", required = true) @RequestParam(value = "verifyToken", required = true) String verifyToken,
			HttpServletResponse response) throws IOException {
		String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
//		String verifyToken = cookieService.getCookie(request, "verifyToken");
//		String verifyToken =(String) request.getAttribute("verifyToken");
		System.out.println("verifyToken=" + verifyToken);
		userService.modifyEmailOrPhoneWithVerifyCode(Constant.checkMethodByEmail, verifyToken, verifyCode);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("img", CodeImageGenerator.generateBase64Image(verifyCode));
		map.put("verifyToken", verifyToken + "," + Constant.verifyCodeForTempValidTime);
		return MapConvert.getMap(ResultType.SUCCESS, map);
	}

	@RequestMapping(value = "/modifyEmailWithSendNewPhoneCode", method = RequestMethod.POST)
	@ApiOperation(value = "修改邮箱流程五：发送新邮箱验证码")
	public Map<String, Object> modifyEmailWithSendNewPhoneCode(HttpServletRequest request, HttpServletResponse response,
			@ApiParam(value = "重定向URL", required = true) @RequestParam(value = "redirectUrl", required = true) String redirectUrl,
			@ApiParam(value = "新邮箱", required = true) @RequestParam(value = "newEmail", required = true) String newEmail,
			@ApiParam(value = "验证码", required = true) @RequestParam(value = "verifyCode", required = true) String verifyCode,
			@ApiParam(value = "令牌", required = true) @RequestParam(value = "verifyToken", required = true) String verifyToken) throws IOException {
//		LocalUtil.set(redirectUrl);
		request.setAttribute("redirectUrl", redirectUrl);
		if (userService.modifyEmailOrPhoneWithSendCode(newEmail, verifyToken, verifyCode,request)) {
			return MapConvert.getMap(ResultType.SUCCESS);
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);
	}

	@RequestMapping(value = "/modifyEmailWithCheckNewPhoneCode", method = RequestMethod.POST)
	@ApiOperation(value = "修改邮箱流程六：校验新邮箱验证码")
	public Map<String, Object> modifyEmailWithCheckNewPhoneCode(HttpServletRequest request, HttpServletResponse response,
			@ApiParam(value = "令牌token", required = true) @RequestParam(value = "verifyToken", required = true) String verifyToken,
			@ApiParam(value = "新邮箱", required = true) @RequestParam(value = "newEmail", required = true) String newEmail,
			@ApiParam(value = "短信验证码", required = true) @RequestParam(value = "verifyCode", required = true) String verifyCode) throws IOException {
		if (userService.modifyEmailOrPhoneWithCheckCode(newEmail, verifyToken, verifyCode)) {
			return MapConvert.getMap(ResultType.SUCCESS);
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);
	}

	@RequestMapping(value = "/forgetPasswdByPhoneWithVerifyCode", method = RequestMethod.GET)
	@ApiOperation(value = "忘记密码通过手机号验证流程一：获取验证码")
	public Map<String, Object> forgetPasswdByPhoneWithVerifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String verifyToken = TokenGenerator.getToken();
		String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
		userService.checkAuthWithVerifyCode(Constant.checkMethodByPhone, verifyToken, verifyCode);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("img", CodeImageGenerator.generateBase64Image(verifyCode));
		map.put("verifyToken", verifyToken + "," + Constant.verifyCodeForTempValidTime);
		return MapConvert.getMap(ResultType.SUCCESS, map);
	}

	@RequestMapping(value = "/forgetPasswdByPhoneWithSendPhoneCode", method = RequestMethod.POST)
	@ApiOperation(value = "忘记密码通过手机号验证流程二：发送短信验证码")
	public Map<String, Object> forgetPasswdByPhoneWithSendPhoneCode(HttpServletRequest request, HttpServletResponse response,
			@ApiParam(value = "手机号", required = true) @RequestParam(value = "phone", required = true) String phone,
			@ApiParam(value = "验证码", required = true) @RequestParam(value = "verifyCode", required = true) String verifyCode,
			@ApiParam(value = "令牌", required = true) @RequestParam(value = "verifyToken", required = true) String verifyToken) throws IOException {
		if (userService.checkAuthWithSendCode(phone, verifyToken, verifyCode,request)) {
			return MapConvert.getMap(ResultType.SUCCESS);
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);
	}

	@RequestMapping(value = "/forgetPasswdByPhoneWithCheckPhoneCode", method = RequestMethod.POST)
	@ApiOperation(value = "忘记密码通过手机号验证流程三：校验短信验证码")
	public Map<String, Object> forgetPasswdByPhoneWithCheckPhoneCode(HttpServletRequest request, HttpServletResponse response,
			@ApiParam(value = "手机号", required = true) @RequestParam(value = "phone", required = true) String phone,
			@ApiParam(value = "短信验证码", required = true) @RequestParam(value = "verifyCode", required = true) String verifyCode,
			@ApiParam(value = "令牌", required = true) @RequestParam(value = "verifyToken", required = true) String verifyToken) throws IOException {
		if (userService.checkAuthWithCheckCode(phone, verifyToken, verifyCode)) {
			return MapConvert.getMap(ResultType.SUCCESS);
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);
	}

	@RequestMapping(value = "/forgetPasswdByPhoneWithCommitNewPasswd", method = RequestMethod.POST)
	@ApiOperation(value = "忘记密码通过手机号验证流程四：提交新密码")
	public Map<String, Object> forgetPasswdByPhoneWithCommitNewPasswd(HttpServletRequest request, HttpServletResponse response,
			@ApiParam(value = "新密码", required = true) @RequestParam(value = "newPasswd", required = true) String newPasswd,
			@ApiParam(value = "令牌", required = true) @RequestParam(value = "verifyToken", required = true) String verifyToken) throws IOException {
		if (userService.modifyPasswdByEmailOrPhone(newPasswd, verifyToken)) {
			return MapConvert.getMap(ResultType.SUCCESS);
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);
	}

	@RequestMapping(value = "/forgetPasswdByEmailWithVerifyCode", method = RequestMethod.GET)
	@ApiOperation(value = "忘记密码通过邮箱验证流程一：获取验证码")
	public Map<String, Object> forgetPasswdByEmailWithVerifyCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String verifyToken = TokenGenerator.getToken();
		String verifyCode = VerifyCodeUtils.generateVerifyCode(4);
		userService.checkAuthWithVerifyCode(Constant.checkMethodByEmail, verifyToken, verifyCode);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("img", CodeImageGenerator.generateBase64Image(verifyCode));
		map.put("verifyToken", verifyToken + "," + Constant.verifyCodeForTempValidTime);
		return MapConvert.getMap(ResultType.SUCCESS, map);
	}

	@RequestMapping(value = "/forgetPasswdByEmailWithSendPhoneCode", method = RequestMethod.POST)
	@ApiOperation(value = "忘记密码通过邮箱验证流程二：发送邮箱验证码")
	public Map<String, Object> forgetPasswdByEmailWithSendPhoneCode(HttpServletRequest request, HttpServletResponse response,
			@ApiParam(value = "重定向URL", required = true) @RequestParam(value = "redirectUrl", required = true) String redirectUrl,
			@ApiParam(value = "邮箱", required = true) @RequestParam(value = "email", required = true) String email,
			@ApiParam(value = "验证码", required = true) @RequestParam(value = "verifyCode", required = true) String verifyCode,
			@ApiParam(value = "令牌", required = true) @RequestParam(value = "verifyToken", required = true) String verifyToken) throws IOException {
//		LocalUtil.set(redirectUrl);
		request.setAttribute("redirectUrl", redirectUrl);
		if (userService.checkAuthWithSendCode(email, verifyToken, verifyCode,request)) {
			return MapConvert.getMap(ResultType.SUCCESS);
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);
	}

	@RequestMapping(value = "/forgetPasswdByEmailWithCheckPhoneCode", method = RequestMethod.POST)
	@ApiOperation(value = "忘记密码通过邮箱验证流程三：校验邮箱验证码")
	public Map<String, Object> forgetPasswdByEmailWithCheckPhoneCode(HttpServletRequest request, HttpServletResponse response,
			@ApiParam(value = "令牌token", required = true) @RequestParam(value = "verifyToken", required = true) String verifyToken,
			@ApiParam(value = "邮箱", required = true) @RequestParam(value = "email", required = true) String email,
			@ApiParam(value = "邮箱验证码", required = true) @RequestParam(value = "verifyCode", required = true) String verifyCode) throws IOException {
		if (userService.checkAuthWithCheckCode(email, verifyToken, verifyCode)) {
			return MapConvert.getMap(ResultType.SUCCESS);
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);
	}

	@RequestMapping(value = "/forgetPasswdByEmailWithCommitNewPasswd", method = RequestMethod.POST)
	@ApiOperation(value = "忘记密码通过邮箱验证流程四：提交新密码")
	public Map<String, Object> forgetPasswdByEmailWithCommitNewPasswd(HttpServletRequest request, HttpServletResponse response,
			@ApiParam(value = "新密码", required = true) @RequestParam(value = "newPasswd", required = true) String newPasswd,
			@ApiParam(value = "令牌", required = true) @RequestParam(value = "verifyToken", required = true) String verifyToken) throws IOException {
		if (userService.modifyPasswdByEmailOrPhone(newPasswd, verifyToken)) {
			return MapConvert.getMap(ResultType.SUCCESS);
		}
		return MapConvert.getMap(ResultType.TOKEN_ERROR);
	}

	/**
	 * 添加反馈意见
	 * 
	 * @param feedBack
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addFeedBack", method = RequestMethod.POST)
	@ApiOperation(value = "用户添加反馈意见", httpMethod = "POST", notes = "用户添加反馈意见,用户必须登录过")
	public Map<String, Object> addFeedBack(@ModelAttribute @Valid Feedback feedBack,HttpServletRequest request) {
		String userId = (String) ((Map<String, Object>) request.getAttribute("userInfo")).get("userId");
		feedBack.setUserId(userId);
		userService.addFeedBack(feedBack);
		return MapConvert.getMap(ResultType.SUCCESS);
	}

	/**
	 * 校验令牌
	 * 
	 * @param feedBack
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/checkAuthToken", method = RequestMethod.POST)
	@ApiOperation(value = "校验令牌", httpMethod = "POST")
	public Map<String, Object> checkAuthToken(HttpServletRequest request) {
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId")) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", userInfo.get("userId"));
			return MapConvert.getMap(ResultType.SUCCESS, map);
		}
		return MapConvert.getMap(ResultType.REQUEST_PARAMS_ERROR);
	}

	/**
	 * 登出
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/logOut", method = RequestMethod.POST)
	@ApiOperation(value = "登出", httpMethod = "POST")
	public Map<String, Object> logOut(HttpServletRequest request) {
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId")) {
			String userId = (String) userInfo.get("userId");
			userService.logOut(userId);
			return MapConvert.getMap(ResultType.SUCCESS);
		}
		return MapConvert.getMap(ResultType.REQUEST_PARAMS_ERROR);
	}
	

	/**
	 * 获取用户信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getBaseUeserInfo", method = RequestMethod.GET)
	@ApiOperation(value = "获取用户基本信息", httpMethod = "GET")
	public Map<String, Object> getBaseUeserInfo(HttpServletRequest request) {
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId")) {
			String userId = (String) userInfo.get("userId");
			return MapConvert.getMap(ResultType.SUCCESS, userService.getBaseUserInfo(userId));
		}
		return MapConvert.getMap(ResultType.REQUEST_PARAMS_ERROR);
	}
	
	
	/**
	 * 获取用户认证信息
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/getCertification", method = RequestMethod.GET)
	@ApiOperation(value = "获取用户认证信息", httpMethod = "GET")
	public Map<String, Object>getCertification(HttpServletRequest request){
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId")) {
			String userId = (String) userInfo.get("userId");
			return MapConvert.getMap(ResultType.SUCCESS, userService.getCertification(userId));
		}
		return MapConvert.getMap(ResultType.REQUEST_PARAMS_ERROR);
	}
	
	
	/**
	 * 添加用户认证
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequestMapping(value = "/addCertification", method = RequestMethod.POST)
	@ApiOperation(value = "添加用户认证", httpMethod = "POST")
	public Map<String, Object> addCertification(@ModelAttribute @Valid Certification cer,HttpServletRequest request){
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		if (userInfo != null && userInfo.containsKey("userId")) {
			String userId = (String) userInfo.get("userId");
			cer.setUserId(userId);
			return userService.addCertification(cer);
		}
		return MapConvert.getMap(ResultType.REQUEST_PARAMS_ERROR);
	}
}